/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gene.core.util;

import gene.core.Core;
import javax.microedition.lcdui.Graphics;
import gene.core.util.Sprite;

/**
 *
 * @author Vinicius
 */
public class CustomFont {

    private gene.core.util.Sprite font;

    private String stmp="";
/**
 * string to get the content to show
 */
    private short string[];
    private int charW, charH, ix, iy;
    private int ibox_w,ibox_h;
    static final String alphabet = "012345689";
    //static final String alphabet = "!\"%()+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ-\'abcdefghijklmnopqrstuvwxyz";

    private int getIndex(char ch) {

            int index=0;

            if ((index=alphabet.indexOf(ch))>=0) {
                return index+1;
            }
        return -1;
    }

    public CustomFont(Sprite font, int charW, int charH) {

        this.font = font;
        this.charW = charW;
        this.charH = charH;
        //this.ibox_h=charH*4;
        //this.ibox_w=(charW*4)-Core.getInstance().getW();
    }

    public CustomFont(String sstr, int _charW, int _charH) {
        //Core._image = Core.getloadImage(sstr);
        
        font = new Sprite(Core.loadImage(sstr), _charW, _charH);//Image.createImage(sstr);

        Core._image=null;

        charW = _charW;

        charH = _charH;

        sstr = null;

        //this.ibox_h=charH*3;
        //this.ibox_w=(charW*4)-Core.getInstance().getW();
    }
/*
    public CustomFont(int iid, int iw, int ih) {
        this.font = new Sprite(Pack.getImage(iid), iw, ih, 1);
        this.charW = iw;
        this.charH = ih;
        this.ibox_h=ih*3;
        this.ibox_w=(iw*4)-Core._IW;
    }*/

    private void drawChar(int index, int ix, int iy) {

        //System.out.println("*** drawchar idx "+index+" px "+ix+" py "+iy);

        font.setPosition(ix, iy);
        font.setFrame(index);
        font.paint(Sprite.TRANS_NONE);
        
    }

    private int getOffset(int index) {
        return 0;
    }

    public void drawString(String sstring, int ix, int iy) {

        
        int screenW = Core.getInstance().getW();

        //System.out.println(" entrada "+sstring+" sstring.lng "+sstring.length()+" ix "+ix+" iy "+iy);

        short atmp[] = {0};

        int tmpy=0;
        /*verifica se a string impressa anteriormente não é igual a
          que pretende ser impressa hoje
         */
        if (!sstring.equals(stmp)) {

            string = null;
            //cria um vetor com o mesmo tamanho da string
            string = new short [sstring.length()];

            //varre a lista ate o fim
            for (int localIndex = 0,tmpx=0; localIndex < sstring.length(); localIndex++,tmpx++) {

                //salva o indice
                atmp[0] =(short)getIndex(sstring.charAt(localIndex));

                //testa o indice
                if (atmp[0] > -1) {
                    
                    if(localIndex >  screenW / charW){
                        tmpx=0;
                        tmpy++;
                    }

                    drawChar(atmp[0],ix+(tmpx* this.charW), iy + (tmpy * this.charH));

                }
                    string[localIndex] = atmp[0];
               
            }
            stmp = sstring;
        } else {
            
            for (int localIndex = 0,tmpx=0; localIndex < string.length; localIndex++,tmpx=0) {

                if (string[localIndex] > -1) {

                    if(localIndex > screenW / charW){
                        tmpx=0;
                        tmpy++;
                    }

                    drawChar(string[localIndex], ix +(tmpx * this.charW), iy +(tmpy * this.charH));
                }
            }
        }
        atmp = null;
        sstring = null;
    }
}
